package com.lzs.Service.Impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lzs.DAO.MenuDAO;
import com.lzs.PO.Menu;
import com.lzs.PO.Page;
import com.lzs.PO.User;
import com.lzs.Service.MenuService;

@Service("MenuService")
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuDAO menuDAO;
	
	public List<Menu> selectMenus() {
		List<Menu> menus = this.menuDAO.selectMenus();
		return menus;
	}

	//递归查询权限信息节点
	public void queryChildMenus(Menu parent, Integer roleid) {
		
		 List<Menu> childmenus = this.menuDAO.queryChildMenus(parent.getMenuid());
		 
		 List<String> menuids = this.menuDAO.findMenuIdsByRoleId(roleid);
		 
		 for(Menu menu: childmenus) {
			 
			 if(menuids.contains(menu.getMenuid())) {
				 
				 menu.setChecked(true);
				 
			 }else {
				 
				 menu.setChecked(false);
				 
			 }
			 
			 queryChildMenus(menu,roleid);
			 
		 }
		 
		 parent.setChildren(childmenus);
		 
	 }

	@Override
	public Page<Menu> findMenuPageList(String keyword, Integer pageSize, Integer pageNum) {
		
		List<Menu> menus = this.menuDAO.findMenuList(keyword);
		
		int totalRecord = menus.size();
		
		Page<Menu> page = new Page<Menu>(pageNum,pageSize,totalRecord);
		
		int startIndex = page.getStartIndex();
		
		page.setList(this.menuDAO.findMenuPageList(keyword, pageSize, startIndex));
		
		return page;
	}

	@Override
	public Boolean countMenuById(String menuid) {
		
		long count = this.menuDAO.countMenuById(menuid);
		
		return count==0;
	}

	@Override
	public void addMenu(Map<String, Object> map) {
		this.menuDAO.addMenu(map);
		
	}

	@Override
	public Menu selectMenuById(String menuid) {
		Menu menu = this.menuDAO.selectMenuById(menuid);
		return menu;
	}

	@Override
	public void deleteMenu(String menuid) {
		
		this.menuDAO.deleteMenu(menuid);
		
	}

	@Override
	public void updateMenu(Map<String, Object> map) {
		this.menuDAO.updateMenu(map);
		
	}


	


	public List<Menu> queryMenus4User(User user) {
     
		List<Menu> menus = this.menuDAO.queryMenus4User(user);
		
		return menus;
		
}

	@Override
	public List<Menu> findRoot(String pid) {
		List<Menu>roolist = this.menuDAO.findRoot(pid);
		return roolist;
	}


//	public void queryChildMenus4User(Menu parent, User user) {
//		
//		List<Menu> childmenus = this.menuDAO.queryChildMenus4User(parent.getMenuid(),user);
//		 
//		 
//		 for(Menu menu: childmenus) {
//			 
//			
//			 
//			 queryChildMenus4User(menu,user);
//			 
//		 }
//		 
//		 parent.setChildren(childmenus);
//		 
//	}

}



